<template>
  <div>
    <div v-if="searchStatus" class="box-shadow pad10 mgb10 flex jbetween">
      <div>
        <el-form
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          label-width="70px"
          class="fnmb talj"
        >
          <el-form-item label="单据号">
            <el-input
              v-model="queryParams.Order"
              placeholder="请输入"
              clearable
              style="width: 212px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="课程名称">
            <el-input
              v-model="queryParams.Name"
              placeholder="请输入"
              clearable
              style="width: 212px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="授课形式">
            <el-select
              v-model="queryParams.Mode"
              clearable
              placeholder="请选择"
              style="width: 212px"
            >
              <el-option label="理论" value="理论" />
              <el-option label="实操" value="实操" />
            </el-select>
          </el-form-item>
          <el-form-item label="授课教师">
            <Selector
              v-model="queryParams.Teacher"
              style="width: 212px"
              category="用户列表"
              :query="{
                Type: 2
              }"
            ></Selector>
          </el-form-item>
          <el-form-item label="流程状态">
            <el-select v-model="queryParams.State" placeholder="请选择" clearable >
              <el-option label="待初审" value="1"></el-option>
              <el-option label="待试讲" value="2"></el-option>
              <el-option label="待终审" value="3"></el-option>
              <el-option label="已关闭" value="4"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="关闭结果">
            <el-select v-model="queryParams.CloseResult" placeholder="请选择" clearable >
              <el-option label="初审不通过" value="1"></el-option>
              <el-option label="终审不通过" value="2"></el-option>
              <el-option label="审核通过" value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="flex acenter jend fnw">
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <MyTable
      :tableList="tableList"
      :total="Total"
      :columns="columns"
      v-model:page="page"
      v-model:searchStatus="searchStatus"
      :btnList="[]"
      @getList="getList"
      @link="handleLink"
    >
    </MyTable>

    <!-- 详情 -->
    <MyDialog
      v-model:show="ShowCourseDetail"
      title="课程详情"
      :width="1000"
      :showFooter="false"
    >
      <CourseDeveDetail :DetailData="CourseDetailData" />
    </MyDialog>
  </div>
</template>

<script setup name="AllDeveList">
import { ref, getCurrentInstance, onMounted } from 'vue';
import {
  GetDevCourseList_Page,
  GetDevCourseDetail
} from '@/api/lesson/lessonDev.js';
import CourseDeveDetail from '@/views/lesson/Development/CourseDeveDetail';
const { proxy } = getCurrentInstance();
const tableList = ref([]);
const Total = ref(0);
const columns = ref([
  { label: '序号' },
  { label: '单据号', prop: 'Order', link: true },
  { label: '课程名称', prop: 'Name' },
  { label: '授课形式', prop: 'Mode' },
  { label: '授课教师', prop: 'Name_Teacher' },
  { label: '流程状态', prop: 'stateTag', tag: true },
  { label: '关闭结果', prop: 'resultTag', tag: true },
  { label: '创建时间', prop: 'CreateTime' },
//   { label: '操作', width: 160 }
]);
proxy.$formatKey(columns.value);
const page = ref({
  PageIndex: 1,
  PageSize: 10
});
const searchStatus = ref(true);
const queryParams = ref({
  Order: '',
  Name: '',
  Mode: '',
  Teacher: '',
  StartTime: '',
  EndTime: '',
  Place: '',
  NoPage: ''
});
const queryRef = ref();
const show = ref(false);
const title = ref();
const formRef = ref();
const rules = ref({
  Score: [{ required: true, message: '请选择', trigger: 'change' }],
  Comment: [{ required: true, message: '请输入', trigger: 'blur' }]
});
const formData = ref({});
const CourseDetailData = ref({});
const ShowCourseDetail = ref(false);

onMounted(() => {
  getList();
});

async function getList() {
  try {
    let query = {
      ...queryParams.value,
      ...page.value
    };
    let res = await GetDevCourseList_Page(query);
    let StateText = {
      1: '待初审',
      2: '待试讲',
      3: '待终审',
      4: '已关闭'
    };
    let StateColor = {
      1: '',
      2: 'warning',
      3: 'success',
      4: 'danger'
    };
    let ResultText = {
      1: '初审不通过',
      2: '终审不通过',
      3: '审核通过'
    };
    let ResultColor = {
      1: 'danger',
      2: 'danger',
      3: 'success'
    };
    res.data.List?.forEach(item => {
      item.stateTag = [
        {
          name: StateText[item.State],
          type: StateColor[item.State]
        }
      ];
      item.resultTag = [
        {
          name: ResultText[item.Close_Result],
          type: ResultColor[item.Close_Result]
        }
      ];
    });
    tableList.value = res.data.List;
    Total.value = res.data.Total;
  } catch (error) {
    console.log(error);
  }
}

async function handleLink(row) {
  try {
    let res = await GetDevCourseDetail({ ID: row.ID });
    let UserType = {
      1: '学生',
      2: '职员',
      3: '管理员'
    };
    res.data?.Participants?.forEach(item => {
      item.TypeName = UserType[item.Type];
    });
    CourseDetailData.value = res.data;
    ShowCourseDetail.value = true;
  } catch (error) {
    console.log(error);
  }
}

function handleCustom(row) {
  title.value = row.Name;
  formData.value.ID = row.ID;
  show.value = true;
}

function handleQuery() {
  page.value.PageIndex = 1;
  getList();
}
function resetQuery() {
  queryParams.value = {
    Code: '',
    Name: '',
    NoPage: ''
  };
  handleQuery();
}
</script>

<style lang="scss" scoped>
:deep(.el-rate__item) {
  height: 20px;
}
</style>
